<?php
	include $setupdata['core_dir']."/modules/payment/gateway/paypalclass.php";
	$paypalclass = new paypalclass;
	
	if ($act=="save"){
		$paypal_data = array();
		if (isset($_POST["VK_ACC"])) $paypal_data['VK_ACC']=$_POST["VK_ACC"]; else $paypal_data['VK_ACC']="";
		if (isset($_POST["VK_CURR"])) $paypal_data['VK_CURR']=$_POST["VK_CURR"]; else $paypal_data['VK_CURR']="";
		if (isset($_POST["KEY_PRIV"])) $paypal_data['KEY_PRIV']=$_POST["KEY_PRIV"];	else $paypal_data['KEY_PRIV']="";
		if (isset($_POST["KEY_PUBL"])) $paypal_data['KEY_PUBL']=$_POST["KEY_PUBL"]; else $paypal_data['KEY_PUBL']="";
		if (isset($_POST["KEY_PAYPAL"])) $paypal_data['KEY_PAYPAL']=$_POST["KEY_PAYPAL"]; else $paypal_data['KEY_PAYPAL']="";
		if (isset($_POST["VK_PASS"])) $paypal_data['VK_PASS']=$_POST["VK_PASS"]; else $paypal_data['VK_PASS']="";
		if (isset($_POST["VK_CERT"])) $paypal_data['VK_CERT']=$_POST["VK_CERT"]; else $paypal_data['VK_CERT']="";

		foreach($paypal_data as $paypal_key=>$paypal_value) {
			if ($paypal_value == "") $err->AddError(25, "payment_paypal");
			else $paypal_data[$paypal_key] = trim($paypal_data[$paypal_key]);
		}

		if (isset($_POST["URL_OK"])) $paypal_data['URL_OK']=trim($_POST["URL_OK"]); else $paypal_data['URL_OK']="";
		if (isset($_POST["URL_CANCEL"])) $paypal_data['URL_CANCEL']=trim($_POST["URL_CANCEL"]); else $paypal_data['URL_CANCEL']="";
		
		$paypal = addslashes(serialize($paypal_data));
		$mysqlclass->mysql_my_query("DELETE FROM `".$setupdata['table_prefix']."local_vars` WHERE 'payment_gateway_paypal'=`name`", __LINE__, __FILE__);
		$mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', 'payment_gateway_paypal', '".$paypal."')", __LINE__, __FILE__);
		
		unset($paypal_tmp);
		unset($paypal_data);
		unset($paypal_key);
		unset($paypal);
	} elseif ($act == "return") {
		$data = $paymentclass->get_gateway_data('paypal');
		if (isset($_GET['result']) && ($_GET['result'] == "notify")){
			$paypalclass->order_notify($_POST);
			$mysqlclass->close_db();
			die();
		} elseif (isset($_GET['result']) && ($_GET['result'] == "ok")){
			$paypalclass->order_notify($_POST);
			if ((isset($data['URL_OK'])) && (strlen($data['URL_OK'])>6)) $str = '"'.$data['URL_OK'].'"';
			else $str = "window.opener.location.href";
		} elseif (isset($_GET['result']) && ($_GET['result'] == "bad")){
			$paypalclass->order_cancel();
			if ((isset($data['URL_CANCEL'])) && (strlen($data['URL_CANCEL'])>6)) $str = '"'.$data['URL_CANCEL'].'"';
			else $str = "window.opener.location.href";
		} else {
			$str = "window.opener.location.href";
		}
		print_r('
			<script language="javascript1.4" type="text/javascript">
				url = '.$str.';
				window.opener.location.replace(url);
				window.close();
			</script>
		');
		$mysqlclass->close_db();
		die();
	}
?>